package com.rexolar.openapi.interceptor;


import com.rexolar.openapi.common.OpenApiResult;
import com.rexolar.openapi.exception.BaseException;
import com.rexolar.openapi.exception.BlockException;
import com.rexolar.openapi.exception.TokenException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhusiyu11000
 * @description: 异常拦截处理器
 * @date 2020-01-09
 */

@RestControllerAdvice
@Slf4j
public class ExceptionControllerAdvice {

    @ExceptionHandler(value = Exception.class)
    public OpenApiResult handleException(Exception e, HttpServletRequest httpServletRequest) {
        //自定义异常
        OpenApiResult resultVO = null;
        if (e instanceof TokenException) {
            BaseException baseOpenException = (BaseException) e;
            //开放平台定义的异常
            resultVO = OpenApiResult.fail(baseOpenException.getErrorCode(), baseOpenException.getMessage());
        } else if (e instanceof BlockException) {
            BlockException blockException = (BlockException) e;
        } else if (e instanceof BaseException) {
            BaseException baseException = (BaseException) e;
            //开放平台定义的异常
            resultVO = OpenApiResult.fail(baseException.getErrorCode(), baseException.getMessage());
        } else {
            log.error("系统异常", e);
        }
        return resultVO;
    }
}
